$(document).ready(function(){
	setupYUITest();
	testMagicTags();
	runAllTests();
});

function setupYUITest(){
	$('body').addClass("yui-skin-sam");
	css = '<style type="text/css">'
		+'.yui-skin-sam .yui-log .pass {background-color:#A7CC25;}'
		+'.yui-skin-sam .yui-log .info {background-color:#cccccc;}'
		+'.yui-skin-sam .yui-log .warn {background-color:orange;}'
		+'.yui-skin-sam .yui-log .error {background-color:red;}'
		+'.yui-skin-sam .yui-log .fail {background-color:red;}'
		+'</style>';
	$('head').append(css);
	//create YUI shortcut
	Assert = YAHOO.util.Assert;
}

function runAllTests(){
	var oLogger = new YAHOO.tool.TestLogger();
    YAHOO.tool.TestRunner.run();
}

//var magictags = new MagicTags();
var magictags = $.MagicTags();
magictags.fadeSpeed = ""; //no fading for the tests

function testMagicTags(){
	var MagicTagsTestCase = new YAHOO.tool.TestCase({
		name: "MagicTags Test Case",
		magictags : magictags,
		
		/* Gets called before every function test */
		setUp : function () {
			//get and store the original html of the testground div
			this.testgroundHTML = $('#testground').html();
			
			//create an instance of the MagicTags class
			//this.magictags = new MagicTags();
        },
    	
		/* Gets called before every function test */
        tearDown : function () {
			//restore the original html for the next test
			$('#testground').html(this.testgroundHTML);
			//delete this.magictags;
        },
		
		testFilterByTag: function(){
			/*
			 * Test that you can filter a set of items linked to a specific magictag set
			 * a magictag set are tags that have a css class that matches the id of the div containing the tags
			 */ 
			//filter items of SetOne by the 'important' tag. 
			this.magictags.filterByTag('mtag-important', 'magictagsSetOne');
			//test that SetOne was affected but SetTwo was not
			var importantTagsSetOne = $('.mtagged.magictagsSetOne.mtag-important:visible').length;
			Assert.areEqual(importantTagsSetOne, 1);
			var setTwoAll = $('.magictagsSetTwo:visible').length;
			Assert.areEqual(8,setTwoAll);
			
			//filter items of SetTwo by the 'important' tag
			this.magictags.filterByTag('mtag-important', 'magictagsSetTwo');
			//test that SetTwo was affected but SetOne was not
			var importantTagsSetTwo = $('.mtagged.magictagsSetTwo.mtag-important:visible').length;
			Assert.areEqual(1,importantTagsSetTwo);
			
			/* Test a tag that doesn't exist */
			this.magictags.filterByTag("spamtag", 'magictagsSetTwo');
			//test that nothing was affected by that action
			Assert.areEqual(1,importantTagsSetTwo);
			Assert.areEqual(1,importantTagsSetOne);
			
			/* Test empty string */
			this.magictags.filterByTag('', 'magictagsSetTwo');
			//test that nothing was affected by that action
			Assert.areEqual(1,importantTagsSetTwo);
			Assert.areEqual(1,importantTagsSetOne);
			
			/* Test wrong object type */
			var badObject = {thing: "bad", reason: "not good"};
			this.magictags.filterByTag(badObject, 'magictagsSetTwo');
			//test that nothing was affected by that action
			Assert.areEqual(1,importantTagsSetTwo);
			Assert.areEqual(1,importantTagsSetOne);
			
			/* Test too many tags passed */
			this.magictags.filterByTag('mtag-adobe-flex mtag-django', 'magictagsSetTwo');
			//test that nothing was affected by that action
			Assert.areEqual(1,importantTagsSetTwo);
			Assert.areEqual(1,importantTagsSetOne);
			
		},
		
		testFilterByOtherTag: function(){
			//SET ONE
			//filter by first tag
			this.magictags.filterByTag('mtag-adobe-flex', 'magictagsSetOne');
			//filter by second tag
			this.magictags.filterByTag('mtag-django' , 'magictagsSetOne');
			
			/*Test that the first filter was replaced by the second */
			//test that there all the django tagged items are visible
			var djangoTags = $('.magictagsSetOne.mtagged.mtag-django:visible').length;
			Assert.areEqual(4,djangoTags);
			
			//test that there are 2 adobe-flex tagged items showing
			var flexTags = $('.magictagsSetOne.mtagged.mtag-adobe-flex:visible').length;
			Assert.areEqual(2, flexTags);
			
			/* Try a third tag */
			this.magictags.filterByTag('mtag-javascript' , 'magictagsSetOne');
			var javascriptTags = $('.magictagsSetOne.mtagged.mtag-javascript:visible').length;
			Assert.areEqual(4,javascriptTags);
			
		},
		
		testRegisterListeners: function(){
			//SET ONE
			/*click on the adobe flex tag and make sure the filtering worked */
			$("#flextag").trigger("click");
			
			//test that all the adobe-flex tagged items are still there
			var flexTaggedSetOne = $(".magictagsSetOne.mtagged.mtag-adobe-flex:visible").length;
			Assert.areEqual(4, flexTaggedSetOne);
			
			//test that items in the first set without adobe-flex tag are hidden
			var nonFlexTagsSetOne = $('.magictagsSetOne.mtagged:not(.mtag-adobe-flex):hidden').length
			Assert.areEqual(4, nonFlexTagsSetOne);
		},
		
		testTagSelection: function(){
			/* click one tag and make sure it is the only 'selected' tag in its group */
			$('#plums-test').trigger("click");
			$('#grapes-test').trigger("click");
			var numSelected = $('.mtagcontrol.selected').length;
			Assert.areEqual(1, numSelected);
			
			/* perform a shift click and test that both are selected */
			
			/* */
		},
		
		testFilterByAdditionalTag: function(){
			//click on one tag, then shift click a second tag. Test for accurate results
			this.magictags.filterByTag('mtag-django', 'magictagsSetOne');
			//additionally filter by second tag
			this.magictags.filterByAdditionalTag('mtag-javascript' , 'magictagsSetOne');
			//test that there are two items remaining in set one, and that they both have
			//javascript and django tags applied
			var remaining = $('.magictagsSetOne.mtagged:visible').length
			Assert.areEqual(2, remaining);
			$('.magictagsSetOne.mtagged:visible').each(function(){
				var item = $(this);
				var hasDjango = $(item).hasClass("mtag-django");
				Assert.areEqual(true, hasDjango);
				var hasJavascript = $(item).hasClass("mtag-javascript");
				Assert.areEqual(true, hasJavascript);
			})
			
		},
		
		testDeselectTag: function(){
			//SET ONE
			//click the first time
			$("#flextag").trigger("click");
			//click the second time
			$("#flextag").trigger("click");
			
			//test that all flex items in the specific set are visible
			var flexTags = $('.magictagsSetOne.mtagged.mtag-adobe-flex:visible').length;
			Assert.areEqual(4, flexTags);
			
			//test that no items in the first set are hidden by adobe flex tag
			var nonFlexTagsSetOne = $('.magictagsSetOne.mtagged:not(.mtag-adobe-flex):hidden').length
			Assert.areEqual(0, nonFlexTagsSetOne);
			
			/* test the global deselect */
			$("#global-important-test").trigger("click");
			$("#global-important-test").trigger("click");
			//test that no items globally are hidden by the 'important' tag
			var nonImportantItems = $('.magictagsSetOne.mtagged:not(.mtag-important):hidden').length
			Assert.areEqual(0, nonImportantItems);
		},
		
		testDeselectAdditionalTag: function(){
			/* Deselecting a tag should still leave in tact other tags previously applied*/
			//click on one tag, then shift click a second tag. Test for accurate results
			this.magictags.filterByTag('mtag-django', 'magictagsSetOne');
			//additionally filter by second tag
			this.magictags.filterByAdditionalTag('mtag-javascript' , 'magictagsSetOne');
			//deselect second tag
			this.magictags.deselectTag('mtag-javascript' , 'magictagsSetOne');
			//test that there are 5 django items still showing, and nothing else in the set
			var setOneItems = $(".magictagsSetOne:visible").length;
			//alert(setOneItems);
			//Assert.areEqual(5, setOneItems);
		},
		
		testClearTagSelection: function(){
			//select some tags
			this.magictags.filterByTag('mtag-django', 'magictagsSetOne');
			
			//clear tags and test that all are visible for the set.
			this.magictags.clearTagSelection('magictagsSetOne');
			var itemsVisibleSetOne = $(".magictagsSetOne:visible").length;
			Assert.areEqual(8, itemsVisibleSetOne); 
			
			
			//test clicking on a 'clear' button
			$("#plums-test").trigger("click");
			$("#setTwoClear").trigger("click");
			var itemsVisibleSetTwo = $(".magictagsSetTwo:visible").length;
			Assert.areEqual(8, itemsVisibleSetTwo); 
			
			
		}
	});
	
	 //add the test cases and suites
    YAHOO.tool.TestRunner.add(MagicTagsTestCase);
}
